<?php
class Model_District_District extends Model_System {
	public $_name = "district";
	protected $_sequence = true;
	
	public function getAll() {
		$result = $this->select();
		return $this->fetchAll($result)->toArray();
	}
	public function getItemId($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		return $this->fetchRow($result)->toArray();
	}
	public function getItemsByProvince($idProvince){
		$result = $this->select();
		$result->where('province_id = ?',$idProvince);
		$result->order("name DESC");
		return $this->fetchAll($result)->toArray();
	}
	public function getItemLangDefault()
	{
		$result = $this->select();
		$result->where('id = ?',1);
		return $this->fetchRow($result)->toArray();
	}
	public function getItemValueByName($id,$name){
		$result = $this->select();
		$result->where('id = ?',$id);
		$data = $this->fetchRow($result)->toArray();
		return $data[$name];
	}
	public function checkAlias($alias)
	{
		$query = $this->select();
		$query->where('alias = ?',$alias);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	static function getNamebyId($id)
	{
		$excute = new Model_District_District();
		$query = $excute->select();
		$query->where('id = ?',$id);
		$result = $excute->fetchRow($query)->toArray();
		return $result['name'];
	}
	public function insertItem($data)
	{
		try {
		    $this->insert($data);
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
		
	}
	public function updateItem($data,$id){
		try {
		    $this->update($data, "id = $id");
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
	}
}